from openai import OpenAI
from common import get_logger
import time
from config import OPENAI_BASE_URL, OPENAI_API_KEY

logger = get_logger(__name__)


def douyin_to_plat_text(douyin_text):
    logger.info(f"准备将抖音文案转成小红书文案,原文案:{douyin_text}")
    client = OpenAI(base_url=OPENAI_BASE_URL, api_key=OPENAI_API_KEY)
    intro_result = client.chat.completions.create(
        model="doubao-1-5-lite-32k-250115",
        messages=[
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": f"请将以下抖音文案改写为小红书爆款正文，正文描述不超过1000字：{douyin_text}",
                    }
                ],
            }
        ],
    )
    plat_intro = intro_result.choices[0].message.content
    time.sleep(1)
    title_result = client.chat.completions.create(
        model="doubao-1-5-lite-32k-250115",
        messages=[
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": f"请根据以下小红书正文生成一个不超过20字的爆款标题：{plat_intro}",
                    }
                ],
            }
        ],
    )
    plat_title = title_result.choices[0].message.content
    time.sleep(1)
    tags_result = client.chat.completions.create(
        model="doubao-1-5-lite-32k-250115",
        messages=[
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": f"请根据以下小红书正文生成5个相关标签，标签用逗号分隔，每个标签不超过10字：{plat_intro}",
                    }
                ],
            }
        ],
    )
    plat_tags = tags_result.choices[0].message.content
    plat_tags = [tag.strip() for tag in plat_tags.split(",") if tag.strip()]
    return plat_title, plat_intro, plat_tags
